package com.kpiJavaTraining.bogdan.module2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

/**
 * class reads a file to test questions
 * @author Olexandr Bogdan
 */
public class InitQuestions {
	private final String PATH = "question.quest";//"C:/output.questions"; //"question.quest";
	private InputStream inputStream;
	private ObjectInputStream objectInputStream;
	private ListOfNode<Question> list;
	
	/**
	 * constructor initiates the field Object
	 * @throws IOException 
	 */
	public InitQuestions() throws IOException{
			inputStream = new FileInputStream(PATH);
			objectInputStream = new ObjectInputStream(inputStream);
			list = new ListOfNode<Question>();	
	}
	
	/**
	 * method reads a file and returns the questions in the form of an ListOfNode
	 * @return ListOfNode
	 * @throws CloneNotSupportedException 
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public ListOfNode<Question> getQuestions() throws IOException, CloneNotSupportedException, ClassNotFoundException {
		Question question;
		while(inputStream.available() > 0 ){
			question = (Question) objectInputStream.readObject();			
			list.add(question);
		}
		return list.clone();
	}

	//private or public?
	private void close() throws IOException{
		objectInputStream.close();
	}
}
